import initdata from "./initdata";
export function reducer(state = initdata, action) {
  // console.log(state, action);
  const { parkTable, bankTable } = state
  switch (action.type) {
    case 'ADDPARK':
      return { parkTable: [...parkTable, action.data] };
    case 'SEARCHPARK':
      return { parkTable: action.data };
    case 'DELPARK':
      return { parkTable: parkTable.filter(item => item.key !== action.data) };
    case 'MODIFYPARK':
      let parkArr = parkTable.concat([])
      console.log(parkArr);
      parkArr.forEach((item) => {
        if (item.key === action.data.key) {
          for (let k in action.data) {
            item[k] = action.data[k]
          }
        }
      })
      console.log(parkTable, parkArr)
      return { parkTable: parkArr }
    case 'ADDBANK':
      return { bankTable: [...bankTable, action.data] };
    case 'SEARCBANK':
      return { bankTable: action.data };
    case 'DELBANK':
      return { bankTable: bankTable.filter(item => item.key !== action.data) };
    case 'MODIFYBANK':
      let bankArr = bankTable.concat([])
      console.log(bankArr);
      bankArr.forEach((item) => {
        if (item.key === action.data.key) {
          for (let k in action.data) {
            item[k] = action.data[k]
          }
        }
      })
      console.log(bankTable, bankArr)
      return { bankTable: bankArr }


    // lzm
    case 'GETCOMMESS':
      console.log(action.num)
      let comdata = []
      state.comMessdata.forEach(item => {
        if (item.comID.indexOf(action.num.comID) >= 0 || item.comName.indexOf(action.num.comname) >= 0 || item.userID.indexOf(action.num.userID) >= 0) {
          comdata.push(item)
        }
      })
      return {
        ...state, comMessdatas: comdata
      }

    case 'GETCORPAGE':
      let cordata = []
      console.log(action.num)
      state.invoices.forEach(item => {
        if (item.key == action.num.key && action.num.key) {
          cordata.push(item)
        } else if (item.comName == action.num.comName && action.num.comName) {
          cordata.push(item)
        }
      })
      console.log(cordata)
      return { ...state, invoicess: cordata ? cordata : invoices }
    case 'AMENDPAGE':
      let initdata = []
        for(let i = 0; i < state.invoices.length; i++){
          // console.log(state.invoices[i].key)
          // console.log(action.num.key)
          if(state.invoices[i].key == action.num.key){
            // console.log(1111)
            state.invoices[i]={...action.num}
          }
          initdata.push( state.invoices[i])
        }
      return{ ...state,invoicess:initdata}
    // 周伟
    case 'CHANGEPAGE':
      return { ...state, page: action.p };
    case 'CHANGEPAGESIZE':
      return { ...state, pagesize: action.pz }
    case 'ADDSTANDARDDATA':
      return { ...state, standarddata: [...state.standarddata, action.obj] }
    case 'EDITSTANDARDDATA':
      return {
        ...state, standarddata: action.arr
      }
    case 'ENABLE':
      return {
        ...state, standarddata: action.Earr
      }
    case 'DISABLE':
      return {
        ...state, standarddata: action.Darr
      }
    case 'DELETEF':
      console.log(action.a);
      return { ...state, standarddata: action.a }
    case 'QUERY':
      return { ...state, standarddata: action.q }
    default:
      return { ...state };
  }
}